package cn.antia.cep.common;

import java.security.MessageDigest;

/**
 * 简单的密码加密提供类
* User: xiao
 * Date: 2006-12-21
 * Time: 09:34:00
 * Version:0.8
 * */
public class SampleEncryption {
	/**
	 * 提供加密后的字符串(使用JDK提供的MD5加密算法加密)
	 * @param primalString 原始字符串
	 * @return 加密后的字符串
	 * @throws AntiaException a*/
	public static String getEncryptString(String primalString)throws AntiaException{
		String result;
		try {
			java.security.MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] input = primalString.getBytes();
			byte[] bt = md.digest(input);
			result = stringify(bt);
			result = xx(result);
			return result;
//			   MD5 md5 = new MD5(primalString);
//			   md5.processString();
//			   result = md5.getStringDigest();
		   }catch(Exception e){
			   e.printStackTrace();
			   throw new AntiaException("001",e.getMessage(),false);
		   }
	}
	
	private static String stringify(byte buf[]) {
	    StringBuffer sb = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            byte aBuf = buf[i];
            int h = (aBuf & 0xf0) >> 4;
            int l = (aBuf & 0x0f);
            sb.append(new Character((char) ((h > 9) ? 'a' + h - 10 : '0' + h)));
            sb.append(new Character((char) ((l > 9) ? 'a' + l - 10 : '0' + l)));
        }
        return sb.toString();
	  }
	
	private static String xx(String s){
		int l = s.length();
		StringBuffer sb = new StringBuffer();
		for(int i = l - 1;i >= 0;i--){
			char a = s.charAt(i);
			sb.append(a);
		}
		return sb.toString();
	}
	
	public static void main(String[] args){
		try{
            String a = SampleEncryption.getEncryptString("admin");
			System.out.println(a);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
